/**
 * OperationLogServiceImpl.java
 * @author
 * 作者 屈甲康 
 */
package org.jackysoft.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jackysoft.data.SortInfo;
import org.jackysoft.entity.nosql.OperationLog;
import org.jackysoft.service.AbstractNoSqlServiceImpl;
import org.jackysoft.service.OperationLogService;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

@Service
public class OperationLogServiceImpl extends AbstractNoSqlServiceImpl<OperationLog> implements
		OperationLogService {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8233676807353168745L;

	public OperationLogServiceImpl() {
		super(OperationLog.class);
	}

	@Override
	protected List<OperationLog> searchData(int start, int size,
			boolean listAll, SortInfo sortInfo, Map<String, Object> smap) {
		
		String date = ((String[]) smap.get("createDate"))[0];
		String details = ((String[]) smap.get("details"))[0];
		
		
		if((!StringUtils.isBlank(details))){
		   Pattern pattern = Pattern.compile(".*"+details+"%'",Pattern.CASE_INSENSITIVE);
		   DBObject query = new BasicDBObject("details",pattern);
		   query.put("$sort", new BasicDBObject("createDate","desc"));
		   DBCursor dbcur = getCollection().find(query).skip(start).limit(size);
		   List<DBObject> list = dbcur.toArray();
		   return this.fromMongodbList(list);
		   
		}
		
		return Collections.EMPTY_LIST;
	}

	@Override
	protected int searchDataSize(SortInfo sortInfo, Map<String, Object> smap) {
		String date = ((String[]) smap.get("createDate"))[0];
		String details = ((String[]) smap.get("details"))[0];
		Pattern pattern = Pattern.compile("^.*"+details+".*$");
		DBObject query = new BasicDBObject("details",pattern);
	    Long count = getCollection().count(query);
		return count.intValue();
	}

	
		

}